package lookas;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class DatabaseToFile{

    public static void main(String[] args) throws Exception{
        Class.forName("org.apache.derby.jdbc.ClientDriver");
        Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/db");

        FileWriter f = new FileWriter("imiona.txt");
        BufferedWriter b = new BufferedWriter(f);
        PreparedStatement stmt = con.prepareStatement("select imie, licznosc from imiona where licznosc>0");
        ResultSet res = stmt.executeQuery();
        while(res.next()){
            String name = res.getString(1);
            StringBuilder str = new StringBuilder(name);
            str.append(";");
            int value = res.getInt(2);
            str.append(value);
            str.append("\n");
            b.write(str.toString());
            b.flush();
        }
        stmt.close();
        con.close();
        f.close();
        System.out.println("ok");
    }
}
